package com.wzy.new_energy_car_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.new_energy_car_platform.common.GlobalVariable;
import com.wzy.new_energy_car_platform.common.ResultDto;
import com.wzy.new_energy_car_platform.dao.SysUserMapper;
import com.wzy.new_energy_car_platform.model.SysUser;
import com.wzy.new_energy_car_platform.model.UserCar;
import com.wzy.new_energy_car_platform.service.SysUserService;
import com.wzy.new_energy_car_platform.utils.JwtUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzy
 * @since 2024-02-06
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public ResultDto<?> getUser(String token) {
        if (Strings.isBlank(token)){
            return ResultDto.error("token不能为空！");
        }
        Map<String,Object> user = JwtUtils.getUser(token);
        if (Objects.isNull(user)){
            return ResultDto.error("手机号或密码错误！");
        }
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserLambdaQueryWrapper.eq(SysUser::getPhoneNumber,user.get("phoneNumber"))
                .eq(SysUser::getPassword,user.get("password"))
                .eq(SysUser::getValidFlag, GlobalVariable.VALID_TRUE);

        SysUser sysUser = sysUserMapper.selectOne(sysUserLambdaQueryWrapper);
        return ResultDto.ok(sysUser);

    }

    @Override
    public ResultDto<?> login(SysUser sysUser) {
        if (Objects.nonNull(sysUser)){
            if (Strings.isBlank(sysUser.getPhoneNumber()) || Strings.isBlank(sysUser.getPassword())){
                return ResultDto.error("手机号或密码不能为空！");
            }
        }
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserLambdaQueryWrapper.eq(SysUser::getPhoneNumber,sysUser.getPhoneNumber())
                .eq(SysUser::getPassword,sysUser.getPassword())
                .eq(SysUser::getValidFlag,GlobalVariable.VALID_TRUE);
        SysUser user = sysUserMapper.selectOne(sysUserLambdaQueryWrapper);
        if (Objects.isNull(user)){
            return ResultDto.error("用户名或密码错误！");
        }
        String token = JwtUtils.generateToken(sysUser.getPhoneNumber(),sysUser.getPassword());
        return ResultDto.ok("登陆成功！",token);
    }

    @Override
    public ResultDto<?> setUser(SysUser sysUser) {
        sysUserMapper.updateById(sysUser);
        return ResultDto.ok();
    }

    @Override
    public ResultDto<?> uploadHeader(Map<String, Object> map) {
        LambdaUpdateWrapper<SysUser> sysUserLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        sysUserLambdaUpdateWrapper.eq(SysUser::getId,map.get("id"))
                .eq(SysUser::getValidFlag,GlobalVariable.VALID_TRUE)
                .set(SysUser::getImg,map.get("img"));
        sysUserMapper.update(null,sysUserLambdaUpdateWrapper);
        return ResultDto.ok();
    }

    @Override
    public ResultDto<?> register(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserLambdaQueryWrapper.eq(SysUser::getPhoneNumber,sysUser.getPhoneNumber())
                .eq(SysUser::getValidFlag,GlobalVariable.VALID_TRUE);
        SysUser user = sysUserMapper.selectOne(sysUserLambdaQueryWrapper);
        if (Objects.nonNull(user)){
            return ResultDto.error("该手机号已注册！");
        }
        sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserLambdaQueryWrapper.eq(SysUser::getUserName,sysUser.getUserName())
                .eq(SysUser::getValidFlag,GlobalVariable.VALID_TRUE);
        user = sysUserMapper.selectOne(sysUserLambdaQueryWrapper);
        if (Objects.nonNull(user)){
            return ResultDto.error("该用户名已存在！");
        }

        sysUser.setValidFlag(GlobalVariable.VALID_TRUE);
        sysUserMapper.insert(sysUser);
        return ResultDto.ok("注册成功！");
    }


}
